﻿using Client.ServerServices;
using System;
using System.Collections.Generic;
using System.ServiceModel;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;

namespace Client
{
    /// <summary>
    /// Interaction logic for Game.xaml
    /// </summary>

    public partial class Game : UserControl
    {
        #region Fields
        private int circleSize = 54;
        private Ellipse activePiece;
        private string tableID;
        private bool isTheFirstPlayer;
        private string opponentName;
        private bool iHaveToStep;
        private MainWindow mainWindow;
        public string ChatMessageTextBox { get; set; }
        #endregion

        #region Constructors
        public Game()
        {
            InitializeComponent();
        }

        public Game(MainWindow mainWindow, string tableID1, bool isThatTheFirstPlayer, string opponentName, bool cont)
        {
            InitializeComponent();
            this.mainWindow = mainWindow;
            this.tableID = tableID1;
            this.isTheFirstPlayer = isThatTheFirstPlayer;
            this.opponentName = opponentName;
            AddPieces();
            if (!cont)
            {
                if (isThatTheFirstPlayer)
                {
                    ChatText.Items.Add("Waiting for oppponent");
                    OpponentInformation.Text = "You playing against: ";
                    ChatSendButton.IsEnabled = false;
                    CloseGame.IsEnabled = true;
                    GiveUpGame.IsEnabled = false;
                }
                else
                {
                    iHaveToStep = false;
                    TurnInformation.Text = "It's your opponent turn!";
                    OpponentInformation.Text = "You playing against: " + opponentName + "!";
                    CloseGame.IsEnabled = false;
                    GiveUpGame.IsEnabled = true;
                    ChatSendButton.IsEnabled = true;
                }
            }
        }

        private void AddPieces()
        {
            FirstPieces = new List<Ellipse>();
            SecondPieces = new List<Ellipse>();
            FirstPieces.Add(FirstPlayerPiece1);
            FirstPieces.Add(FirstPlayerPiece2);
            FirstPieces.Add(FirstPlayerPiece3);
            FirstPieces.Add(FirstPlayerPiece4);
            FirstPieces.Add(FirstPlayerPiece5);
            FirstPieces.Add(FirstPlayerPiece6);
            FirstPieces.Add(FirstPlayerPiece7);
            FirstPieces.Add(FirstPlayerPiece8);
            FirstPieces.Add(FirstPlayerPiece9);
            FirstPieces.Add(FirstPlayerPiece10);
            FirstPieces.Add(FirstPlayerPiece11);
            FirstPieces.Add(FirstPlayerPiece12);
            FirstPieces.Add(FirstPlayerPiece1King);
            FirstPieces.Add(FirstPlayerPiece2King);
            FirstPieces.Add(FirstPlayerPiece3King);
            FirstPieces.Add(FirstPlayerPiece4King);
            FirstPieces.Add(FirstPlayerPiece5King);
            FirstPieces.Add(FirstPlayerPiece6King);
            FirstPieces.Add(FirstPlayerPiece7King);
            FirstPieces.Add(FirstPlayerPiece8King);
            FirstPieces.Add(FirstPlayerPiece9King);
            FirstPieces.Add(FirstPlayerPiece10King);
            FirstPieces.Add(FirstPlayerPiece11King);
            FirstPieces.Add(FirstPlayerPiece12King);
            SecondPieces.Add(SecondPlayerPiece1);
            SecondPieces.Add(SecondPlayerPiece2);
            SecondPieces.Add(SecondPlayerPiece3);
            SecondPieces.Add(SecondPlayerPiece4);
            SecondPieces.Add(SecondPlayerPiece5);
            SecondPieces.Add(SecondPlayerPiece6);
            SecondPieces.Add(SecondPlayerPiece7);
            SecondPieces.Add(SecondPlayerPiece8);
            SecondPieces.Add(SecondPlayerPiece9);
            SecondPieces.Add(SecondPlayerPiece10);
            SecondPieces.Add(SecondPlayerPiece11);
            SecondPieces.Add(SecondPlayerPiece12);
            SecondPieces.Add(SecondPlayerPiece1King);
            SecondPieces.Add(SecondPlayerPiece2King);
            SecondPieces.Add(SecondPlayerPiece3King);
            SecondPieces.Add(SecondPlayerPiece4King);
            SecondPieces.Add(SecondPlayerPiece5King);
            SecondPieces.Add(SecondPlayerPiece6King);
            SecondPieces.Add(SecondPlayerPiece7King);
            SecondPieces.Add(SecondPlayerPiece8King);
            SecondPieces.Add(SecondPlayerPiece9King);
            SecondPieces.Add(SecondPlayerPiece10King);
            SecondPieces.Add(SecondPlayerPiece11King);
            SecondPieces.Add(SecondPlayerPiece12King);
        }

        #endregion

        public void OpponentArrived(string opponent)
        {
            opponentName = opponent;
            front_canvas.IsEnabled = true;
            OpponentInformation.Text = "You playing against: " + opponentName + "!";
            ChatText.Items.Add("Oppponent has arrived: " + opponentName);
            iHaveToStep = true;
            TurnInformation.Text = "It's your turn!";
            ChatSendButton.IsEnabled = true;
            CloseGame.IsEnabled = false;
            GiveUpGame.IsEnabled = true;
        }

        public void EndOfGame()
        {
            CloseGame.Visibility = System.Windows.Visibility.Visible;
            CloseGame.IsEnabled = true;
            GiveUpGame.IsEnabled = false;
            ChatSendButton.IsEnabled = false;
            CloseGame.IsEnabled = true;
        }

        public void TurnChanger()
        {
            TurnInformation.Text = "It's your turn!";
        }

        #region Graphic Methods
        public void MoveTo(double newX, double newY)
        {
            var top = Canvas.GetTop((Grid)activePiece.Parent);
            var left = Canvas.GetLeft((Grid)activePiece.Parent);

            TranslateTransform trans = new TranslateTransform();

            DoubleAnimation anim1 = new DoubleAnimation(0, newY - top, TimeSpan.FromSeconds(1));
            DoubleAnimation anim2 = new DoubleAnimation(0, newX - left, TimeSpan.FromSeconds(1));
            trans.BeginAnimation(TranslateTransform.YProperty, anim1);
            trans.BeginAnimation(TranslateTransform.XProperty, anim2);
            ((Grid)activePiece.Parent).RenderTransform = trans;

        }

        public Ellipse CreateAnEllipse(Brush playerColor)
        {
            SolidColorBrush borderBrush = new SolidColorBrush() { Color = Colors.Yellow };
            return new Ellipse()
            {
                Height = circleSize,
                Width = circleSize,
                StrokeThickness = 0,
                Stroke = borderBrush,
                Fill = playerColor
            };
        }

        static string NumberToAlphabet(int index)
        {
            const string letters = "ABCDEFGH";

            string value = "";
            if (index < 8)
                value += letters[index];

            return value;
        }

        private void Rectangle_MouseLeftButtonUp(object sender, MouseButtonEventArgs e)
        {
            if (activePiece != null)
            {
                ServerServices.StepRequest request = new ServerServices.StepRequest();
                double a = (int)Math.Floor(Canvas.GetLeft((Grid)activePiece.Parent) / 60);
                double b = Math.Floor(Canvas.GetTop((Grid)activePiece.Parent) / 60);
                double c = (int)Math.Floor(Canvas.GetLeft((Rectangle)sender) / 60);
                double d = Math.Floor(Canvas.GetTop((Rectangle)sender) / 60);
                request.StepFrom = new ServerServices.Point();
                request.StepFrom.X = NumberToAlphabet((int)Math.Floor(Canvas.GetLeft((Grid)activePiece.Parent) / 60));
                request.StepFrom.Y = ((Math.Floor(Canvas.GetTop((Grid)activePiece.Parent) / 60) + 1)).ToString();
                request.StepTo = new ServerServices.Point();
                request.StepTo.X = NumberToAlphabet((int)Math.Floor(Canvas.GetLeft((Rectangle)sender) / 60));
                request.StepTo.Y = ((Math.Floor(Canvas.GetTop((Rectangle)sender) / 60) + 1)).ToString();
                request.TableID = tableID;
                request.UserID = Login.username;
                request.Opponent = opponentName;
                request.FirstPlayer = true;

                request.FirstPlayer = isTheFirstPlayer;

                request.TableID = tableID;
                request.UserID = Login.username;
                ServerServices.StepServiceResponse response;

                try
                {

                    if ((response = mainWindow.client.StepProcess(request)).Code < 200)
                    {

                        if (response.Kill)
                        {
                            if (request.FirstPlayer)
                            {
                                foreach (var item in SecondPieces)
                                {
                                    if ((Convert.ToDouble(LetterToNumber(response.X) * 60) == Canvas.GetLeft((Grid)item.Parent)) && ((Convert.ToDouble(response.Y) - 1) * 60 == Canvas.GetTop((Grid)item.Parent)))
                                    {
                                        item.Width = 0;
                                        item.Height = 0;
                                    }
                                }
                            }
                            else
                            {
                                foreach (var item in FirstPieces)
                                {
                                    if ((Convert.ToDouble(LetterToNumber(response.X) * 60) == Canvas.GetLeft((Grid)item.Parent)) && ((Convert.ToDouble(response.Y) - 1) * 60 == Canvas.GetTop((Grid)item.Parent)))
                                    {
                                        item.Width = 0;
                                        item.Height = 0;
                                    }
                                }
                            }
                        }
                        if (response.Code == 108)
                        {
                            /*Ellipse king = (Ellipse)System.Windows.LogicalTreeHelper.FindLogicalNode(
                                ((Grid)activePiece.Parent), activePiece.Name + "King");
                            king.Width = 14;
                            king.Height = 14;*/

                            front_canvas.Children.Remove((Grid)activePiece.Parent);
                            Canvas.SetLeft((Grid)activePiece.Parent, Canvas.GetLeft((Rectangle)sender));
                            Canvas.SetTop((Grid)activePiece.Parent, Canvas.GetTop((Rectangle)sender));
                            front_canvas.Children.Add((Grid)activePiece.Parent);
                            if (response.Kill == true)
                            {
                                DisappearPiece(request, response);
                            }
                            TurnInformation.Text = "It's your opponent turn!";
                        }
                        else if (response.Code == 109)//Ha király lett
                        {
                            Ellipse king = (Ellipse)System.Windows.LogicalTreeHelper.FindLogicalNode(
                                ((Grid)activePiece.Parent), activePiece.Name + "King");
                            king.Width = 14;
                            king.Height = 14;
                            if (response.Kill == true)
                            {
                                DisappearPiece(request, response);
                            }
                            front_canvas.Children.Remove((Grid)activePiece.Parent);
                            Canvas.SetLeft((Grid)activePiece.Parent, Canvas.GetLeft((Rectangle)sender));
                            Canvas.SetTop((Grid)activePiece.Parent, Canvas.GetTop((Rectangle)sender));
                            front_canvas.Children.Add((Grid)activePiece.Parent);
                            TurnInformation.Text = "It's your opponent turn!";
                        }
                        else if (response.Code == 115)//Ha nyertes
                        {
                            MessageBox.Show("Játék vége! A nyertes: " + Login.username);
                            EndOfGame();
                            //ELO számítás
                        }

                    }
                    else
                    {
                        MessageBox.Show("hibás lépés " + response.Message);
                    }
                }
                catch (Exception ex)
                {
                    MessageBox.Show("Hiba a szerverben!");
                    mainWindow.GoToServer();
                }

                activePiece.StrokeThickness = 0;
                activePiece = null;

            }
        }

        private void Piece_MouseLeftButtonDown(object sender, MouseButtonEventArgs e)
        {
            if (((Ellipse)sender).Name.Contains((isTheFirstPlayer) ? "Second" : "First")) // ha az ellenséges bábut fogjuk meg akkor semmi se történik.
            {
                return;
            }
            if (activePiece == null)
            {
                activePiece = (Ellipse)sender;
                activePiece.StrokeThickness = 2;
            }
            else if (((Ellipse)sender).Name.Equals(activePiece.Name))
            {
                activePiece.StrokeThickness = 0;
                activePiece = null;

            }
            else
            {
                activePiece.StrokeThickness = 0;
                activePiece = (Ellipse)sender;
                activePiece.StrokeThickness = 2;
            }
        }

        public void DisappearPiece(StepRequest request, StepServiceResponse response)
        {
            if (response.Kill)
            {
                if (request.FirstPlayer)
                {
                    foreach (var item in SecondPieces)
                    {
                        if ((Convert.ToDouble(LetterToNumber(response.X) * 60) == Canvas.GetLeft((Grid)item.Parent)) && ((Convert.ToDouble(response.Y) - 1) * 60 == Canvas.GetTop((Grid)item.Parent)))
                        {
                            item.Width = 0;
                            item.Height = 0;
                        }
                    }
                }
                else
                {
                    foreach (var item in FirstPieces)
                    {
                        if ((Convert.ToDouble(LetterToNumber(response.X) * 60) == Canvas.GetLeft((Grid)item.Parent)) && ((Convert.ToDouble(response.Y) - 1) * 60 == Canvas.GetTop((Grid)item.Parent)))
                        {
                            item.Width = 0;
                            item.Height = 0;
                        }
                    }
                }
            }
        }

        #endregion

        #region Button Events
        private void ChatSendButton_Click(object sender, RoutedEventArgs e)
        {
            SendMsgRequest request = new SendMsgRequest();
            request.Message = ChatMessageTextBox;
            ChatMessageTextBox = "";
            ChatMessage.Clear();
            request.UserName = Login.username;
            request.TableID = tableID;
            SendMsgServiceResponse response = new SendMsgServiceResponse();
            try
            {
                if ((response = mainWindow.client.SendMsgProcess(request)).Code < 200)
                {
                    ChatText.Items.Add(Login.username + ": " + request.Message);
                    ChatMessage.Text = "";
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show("Hiba a szerverben!");
                mainWindow.GoToServer();
            }

        }

        public void RefreshChat()
        {
            RefreshChatBox.BorderThickness = new Thickness(0);
            RefreshChatBox.BorderBrush = new SolidColorBrush(Colors.White);
            ServerServices.GetMsgRequest request = new ServerServices.GetMsgRequest();
            request.Username = Login.username;
            request.TableId = tableID;

            try
            {

                GetMsgServiceResponse response = mainWindow.client.GetMsgProcess(request);
                if (response.Code == 105)
                {


                    foreach (string item in response.ChatMessage)
                    {
                        ChatText.Items.Add(opponentName + ": " + item);
                    }


                }
            }
            catch (Exception ex)
            {
                MessageBox.Show("Hiba a szerverben!");
                mainWindow.GoToServer();
            }

        }

        //ELO számítás itt pl
        private void GiveUpGame_Click(object sender, RoutedEventArgs e)
        {
            if (MessageBox.Show("Biztos fel akarod adni a játékot?", "Játék feladása", MessageBoxButton.YesNo) == MessageBoxResult.Yes)
            {

                ServerServices.GiveUpRequest request = new ServerServices.GiveUpRequest();
                request.TableID = tableID;
                request.UserID = Login.username;
                ServerServices.GiveUpServiceResponse response;
                try
                {
                    if ((response = mainWindow.client.GiveUpProcess(request)).Code < 200)
                    {
                        mainWindow.SetScreen("lobby");
                    }

                }
                catch (Exception ex)
                {
                    MessageBox.Show("Hiba a szerverben!");
                    mainWindow.GoToServer();
                }

            }

        }

        //ELO számítás itt pl
        private void CloseGame_Click(object sender, RoutedEventArgs e)
        {
            CloseGameRequest request = new CloseGameRequest();
            request.UserName = Login.username;
            request.TableID = tableID;
            CloseGameServiceResponse response = mainWindow.client.CloseGameProcess(request);
            mainWindow.SetScreen("lobby");
        }

        private void RefreshChatBox_Click(object sender, RoutedEventArgs e)
        {
            RefreshChat();
        }

        #endregion

        private void ChatMessage_TextChanged(object sender, TextChangedEventArgs e)
        {
            var textBox = sender as TextBox;
            ChatMessageTextBox = textBox.Text;
        }
        public void SetGame(Position p = null)
        {

            FirstPlayerPiece1.Visibility = System.Windows.Visibility.Hidden;
            FirstPlayerPiece1King.Visibility = System.Windows.Visibility.Hidden;
            FirstPlayerPiece2.Visibility = System.Windows.Visibility.Hidden;
            FirstPlayerPiece2King.Visibility = System.Windows.Visibility.Hidden;
            FirstPlayerPiece3.Visibility = System.Windows.Visibility.Hidden;
            FirstPlayerPiece3King.Visibility = System.Windows.Visibility.Hidden;
            FirstPlayerPiece4.Visibility = System.Windows.Visibility.Hidden;
            FirstPlayerPiece4King.Visibility = System.Windows.Visibility.Hidden;
            FirstPlayerPiece5.Visibility = System.Windows.Visibility.Hidden;
            FirstPlayerPiece5King.Visibility = System.Windows.Visibility.Hidden;
            FirstPlayerPiece6.Visibility = System.Windows.Visibility.Hidden;
            FirstPlayerPiece6King.Visibility = System.Windows.Visibility.Hidden;
            FirstPlayerPiece7.Visibility = System.Windows.Visibility.Hidden;
            FirstPlayerPiece7King.Visibility = System.Windows.Visibility.Hidden;
            FirstPlayerPiece8.Visibility = System.Windows.Visibility.Hidden;
            FirstPlayerPiece8King.Visibility = System.Windows.Visibility.Hidden;
            FirstPlayerPiece9.Visibility = System.Windows.Visibility.Hidden;
            FirstPlayerPiece9King.Visibility = System.Windows.Visibility.Hidden;
            FirstPlayerPiece10.Visibility = System.Windows.Visibility.Hidden;
            FirstPlayerPiece10King.Visibility = System.Windows.Visibility.Hidden;
            FirstPlayerPiece11.Visibility = System.Windows.Visibility.Hidden;
            FirstPlayerPiece11King.Visibility = System.Windows.Visibility.Hidden;
            FirstPlayerPiece12.Visibility = System.Windows.Visibility.Hidden;
            FirstPlayerPiece12King.Visibility = System.Windows.Visibility.Hidden;

            SecondPlayerPiece1.Visibility = System.Windows.Visibility.Hidden;
            SecondPlayerPiece1King.Visibility = System.Windows.Visibility.Hidden;
            SecondPlayerPiece2.Visibility = System.Windows.Visibility.Hidden;
            SecondPlayerPiece2King.Visibility = System.Windows.Visibility.Hidden;
            SecondPlayerPiece3.Visibility = System.Windows.Visibility.Hidden;
            SecondPlayerPiece3King.Visibility = System.Windows.Visibility.Hidden;
            SecondPlayerPiece4.Visibility = System.Windows.Visibility.Hidden;
            SecondPlayerPiece4King.Visibility = System.Windows.Visibility.Hidden;
            SecondPlayerPiece5.Visibility = System.Windows.Visibility.Hidden;
            SecondPlayerPiece5King.Visibility = System.Windows.Visibility.Hidden;
            SecondPlayerPiece6.Visibility = System.Windows.Visibility.Hidden;
            SecondPlayerPiece6King.Visibility = System.Windows.Visibility.Hidden;
            SecondPlayerPiece7.Visibility = System.Windows.Visibility.Hidden;
            SecondPlayerPiece7King.Visibility = System.Windows.Visibility.Hidden;
            SecondPlayerPiece8.Visibility = System.Windows.Visibility.Hidden;
            SecondPlayerPiece8King.Visibility = System.Windows.Visibility.Hidden;
            SecondPlayerPiece9.Visibility = System.Windows.Visibility.Hidden;
            SecondPlayerPiece9King.Visibility = System.Windows.Visibility.Hidden;
            SecondPlayerPiece10.Visibility = System.Windows.Visibility.Hidden;
            SecondPlayerPiece10King.Visibility = System.Windows.Visibility.Hidden;
            SecondPlayerPiece11.Visibility = System.Windows.Visibility.Hidden;
            SecondPlayerPiece11King.Visibility = System.Windows.Visibility.Hidden;
            SecondPlayerPiece12.Visibility = System.Windows.Visibility.Hidden;
            SecondPlayerPiece12King.Visibility = System.Windows.Visibility.Hidden;
            AddPieces();

            //----------
            int j = 0;
            int k = 0;
            for (int i = 0; i < p.X.Length; i++)
            {

                double left = LetterToNumber(p.X[i]);
                double top = Convert.ToDouble(p.Y[i]) - 1;
                if (p.V[i] > 0)//Firstplayer-ződ
                {
                    if (p.V[i] == 1)
                    {
                        Canvas.SetLeft((Grid)FirstPieces[j].Parent, left * 60);
                        Canvas.SetTop((Grid)FirstPieces[j].Parent, top * 60);
                        Canvas.SetLeft((Grid)FirstPieces[12 + j].Parent, left * 60);
                        Canvas.SetTop((Grid)FirstPieces[12 + j].Parent, top * 60);
                        FirstPieces[j].Visibility = System.Windows.Visibility.Visible;
                        FirstPieces[12 + j].Visibility = System.Windows.Visibility.Visible;
                    }
                    else
                    {
                        Canvas.SetLeft((Grid)FirstPieces[j].Parent, left * 60);
                        Canvas.SetTop((Grid)FirstPieces[j].Parent, top * 60);
                        Canvas.SetLeft((Grid)FirstPieces[12 + j].Parent, left * 60);
                        Canvas.SetTop((Grid)FirstPieces[12 + j].Parent, top * 60);
                        FirstPieces[12 + j].Width = 14;
                        FirstPieces[12 + j].Height = 14;
                        FirstPieces[j].Visibility = System.Windows.Visibility.Visible;
                        FirstPieces[12 + j].Visibility = System.Windows.Visibility.Visible;
                    }
                    j++;

                }
                else if (p.V[i] < 0)
                {
                    if (p.V[i] == -1)
                    {
                        Canvas.SetLeft((Grid)SecondPieces[k].Parent, left * 60);
                        Canvas.SetTop((Grid)SecondPieces[k].Parent, top * 60);
                        Canvas.SetLeft((Grid)SecondPieces[12 + k].Parent, left * 60);
                        Canvas.SetTop((Grid)SecondPieces[12 + k].Parent, top * 60);
                        SecondPieces[k].Visibility = System.Windows.Visibility.Visible;
                        SecondPieces[12 + k].Visibility = System.Windows.Visibility.Visible;
                    }
                    else
                    {
                        Canvas.SetLeft((Grid)SecondPieces[k].Parent, left * 60);
                        Canvas.SetTop((Grid)SecondPieces[k].Parent, top * 60);
                        Canvas.SetLeft((Grid)SecondPieces[12 + k].Parent, left * 60);
                        Canvas.SetTop((Grid)SecondPieces[12 + k].Parent, top * 60);
                        SecondPieces[12 + k].Width = 14;
                        SecondPieces[12 + k].Height = 14;
                        SecondPieces[k].Visibility = System.Windows.Visibility.Visible;
                        SecondPieces[12 + k].Visibility = System.Windows.Visibility.Visible;
                    }
                    k++;
                }
            }
            if (p.Turn)
            {
                iHaveToStep = true;
                OpponentInformation.Text = "You playing against: " + opponentName + "!";
                CloseGame.IsEnabled = false;
                GiveUpGame.IsEnabled = true;
                ChatSendButton.IsEnabled = true;
            }
            else
            {
                iHaveToStep = false;
                TurnInformation.Text = "It's your opponent turn!";
                OpponentInformation.Text = "You playing against: " + opponentName + "!";
                CloseGame.IsEnabled = false;
                GiveUpGame.IsEnabled = true;
                ChatSendButton.IsEnabled = true;
            }
        }
        public List<Ellipse> FirstPieces { get; set; }
        public List<Ellipse> SecondPieces { get; set; }
        public double LetterToNumber(string c)
        {
            switch (c)
            {
                case "A": return 0;
                case "B": return 1;
                case "C": return 2;
                case "D": return 3;
                case "E": return 4;
                case "F": return 5;
                case "G": return 6;
                case "H": return 7;
            }
            return -1;
        }

        public void PlacePiece(StepRequest request)
        {
            foreach (var item in FirstPieces)
            {
                if (LetterToNumber(request.StepFrom.X) * 60 == Canvas.GetLeft((Grid)item.Parent))
                {
                    if ((Convert.ToDouble(request.StepFrom.Y) - 1) * 60 == Canvas.GetTop((Grid)item.Parent))
                    {
                        Canvas.SetLeft((Grid)item.Parent, LetterToNumber(request.StepTo.X) * 60);
                        Canvas.SetTop((Grid)item.Parent, (Convert.ToDouble(request.StepTo.Y) - 1) * 60);
                        if (Canvas.GetTop((Grid)item.Parent) == 0)
                        {
                            Ellipse king = (Ellipse)System.Windows.LogicalTreeHelper.FindLogicalNode(
                             ((Grid)item.Parent), item.Name + "King");
                            Canvas.SetLeft((Grid)king.Parent, LetterToNumber(request.StepTo.X) * 60);
                            Canvas.SetTop((Grid)king.Parent, (Convert.ToDouble(request.StepTo.Y) - 1) * 60);
                            king.Width = 14;
                            king.Height = 14;
                        }
                    }
                }
            }
            foreach (var item in SecondPieces)
            {
                if (LetterToNumber(request.StepFrom.X) * 60 == Canvas.GetLeft((Grid)item.Parent))
                {
                    if ((Convert.ToDouble(request.StepFrom.Y) - 1) * 60 == Canvas.GetTop((Grid)item.Parent))
                    {
                        Canvas.SetLeft((Grid)item.Parent, LetterToNumber(request.StepTo.X) * 60);
                        Canvas.SetTop((Grid)item.Parent, (Convert.ToDouble(request.StepTo.Y) - 1) * 60);
                        if (Canvas.GetTop((Grid)item.Parent) == (7 * 60))
                        {
                            Ellipse king = (Ellipse)System.Windows.LogicalTreeHelper.FindLogicalNode(
                                ((Grid)item.Parent), item.Name + "King");
                            Canvas.SetLeft((Grid)king.Parent, LetterToNumber(request.StepTo.X) * 60);
                            Canvas.SetTop((Grid)king.Parent, (Convert.ToDouble(request.StepTo.Y) - 1) * 60);
                            king.Width = 14;
                            king.Height = 14;
                        }
                    }
                }
            }
        }
    }
}
